﻿using Castle.DynamicProxy;


namespace DynamicAgent
{
    /// <summary>
    /// log拦截器
    /// </summary>
    public class LoggerInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            Console.WriteLine("执行方法前");
            //获取执行信息
            var methodName = invocation.Method.Name;
            //调用业务方法
            invocation.Proceed();
            if(invocation.ReturnValue is string)
            {
                invocation.ReturnValue = "修改后的方法" + invocation.ReturnValue;
            }

            if (invocation.ReturnValue is Task<string>)
            {
                invocation.ReturnValue = Task.FromResult<string>("你好中国");
            }
            Console.WriteLine($"执行方法后 {invocation.ReturnValue}");
        }
    }
}
